package com.uzhie.web.service;

import com.github.pagehelper.Page;
import com.uzhie.exception.BaseDataException;
import com.uzhie.util.ConstantKit;
import com.uzhie.web.dao.BaseDataDAO;
import com.uzhie.web.dao.CatalogDAO;
import com.uzhie.web.dao.EquipmentDAO;
import com.uzhie.web.dao.VehicleDAO;
import com.uzhie.web.module.BaseData;
import org.apache.ibatis.session.RowBounds;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 数据字典服务类
 * Created by lilongfei on 2018/7/19.
 */
@Service
public class BaseDataService {

    @Autowired
    BaseDataDAO baseDataDAO;
    @Autowired
    CatalogDAO catalogDAO;
    @Autowired
    VehicleDAO vehicleDAO;
    @Autowired
    EquipmentDAO equipmentDAO;


    // 字典表的状态值 1 启用, 2 禁用
    private Integer[] types = new Integer[]{ConstantKit.STATUS.IS_AVAILABLE, ConstantKit.STATUS.UN_AVAILABLE};

    /**
     * @param type      字典类型,目前定义的字典类型有
     *                  {部门类型 = 1 ,车辆类型= 3,终端类型= 4,报警类型= 5,其他= 6}
     * @param status    查询的数据状态 1 启用, 2 禁用
     * @param rowBounds 分页控件
     * @return 查询出来的字典对象的Page集合
     * @throws BaseDataException
     */
    public Page queryList(@NotEmpty(message = BaseDataException.Status.TYPE_ISNULL) String type, Integer status, RowBounds rowBounds) throws BaseDataException {
        if (rowBounds == null) throw new BaseDataException(BaseDataException.PAGE_ISNULL);
        List<BaseData> list = baseDataDAO.queryListByCondition(type, status, rowBounds);
        return (Page) list;
    }

    public Map<String, List<BaseData>> queryListByTypes(@NotEmpty(message = BaseDataException.Status.TYPE_ISNULL) List<String> types, Integer status, RowBounds rowBounds) throws BaseDataException {
        if (rowBounds == null) throw new BaseDataException(BaseDataException.PAGE_ISNULL);
        Map<String, List<BaseData>> result = new HashMap<>();
        for (String type : types) {
            List<BaseData> list = baseDataDAO.queryListByCondition(type, status, rowBounds);
            for (ConstantKit.BASEDATA data : ConstantKit.BASEDATA.values())
                if (data.getType() == Integer.valueOf(type)) result.put(data.getValue(), list);
        }
        return result;
    }

    /**
     * 更新字典对象的状态
     *
     * @param baseData 需要更新状态的字典对象
     * @param status   需要更新的字典状态
     * @throws BaseDataException
     */
    public void updateStatus(@NotNull(message = BaseDataException.Status.BASE_DATA_ISNULL) @Valid BaseData baseData, int status) throws BaseDataException {
        List<Integer> arr = Arrays.asList(types);
        if (!arr.contains(status)) throw new BaseDataException(BaseDataException.Status.STATUS_ERROR);
        //设置字典对象的状态值与更新时间
        baseData.setStatus(status);
        baseData.setUpdateTime(new Date());
        baseDataDAO.updateByPrimaryKey(baseData);
    }

    /**
     * 删除字典信息
     * 已经被引用的对象不可删除
     * 非禁用状态下的对象不可删除
     *
     * @param data {部门类型 = 1 ,车辆类型= 3,终端类型= 4,报警类型= 5,其他= 6,生产厂家 = 7,车牌颜色 = 8}
     * @throws BaseDataException
     */
    public void deleteBaseData(@NotNull(message = BaseDataException.Status.BASE_DATA_ISNULL) BaseData data) throws BaseDataException {
        if (data.getStatus() != 2) throw new BaseDataException(BaseDataException.Status.STATUS_ERROR);
        //检测当前字典对象是否已经被引用,如果被引用,则不可删除
//        switch (data.getType()){
//            case "1":
//                if (catalogDAO.baseDataHasBeUsed(data.getId())!=0)
//                    throw new BaseDataException(BaseDataException.Status.CANT_DELETE);
//                break;
//            case "3":
//                if (vehicleDAO.baseDataHasBeUsed(data.getId())!=0)
//                    throw new BaseDataException(BaseDataException.Status.CANT_DELETE);
//                break;
//            case "4":
//                if (equipmentDAO.baseDataHasBeUsed(data.getId())!=0)
//                    throw new BaseDataException(BaseDataException.Status.CANT_DELETE);
//                break;
//            case "5":
//                //TODO 删除字典时需要索引检查报警类型是否被引用(未完成)
//                break;
//            case "6":
//                break;
//        }
        baseDataDAO.deleteByPrimaryKey(data.getId());
    }

    /**
     * 新增数据字典
     *
     * @param data
     */
    public void addBaseData(@NotNull(message = BaseDataException.Status.BASE_DATA_ISNULL) @Valid BaseData data) {
        data.setCreateTime(new Date());
        data.setStatus(ConstantKit.STATUS.IS_AVAILABLE);
        baseDataDAO.insert(data);
    }

    /**
     * 修改字典数据
     *
     * @param data
     */
    public void updateBaseData(@NotNull(message = BaseDataException.Status.BASE_DATA_ISNULL) @Valid BaseData data) {
        data.setUpdateTime(new Date());
        baseDataDAO.updateByPrimaryKeySelective(data);
    }
}
